<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>知云——针对编程的个性化学习推荐系统</title>
    <%
        pageContext.setAttribute("APP_PATH", request.getContextPath());
    %>
    <base href="${APP_PATH}">
    <link rel="shortcut icon" type="image/x-icon" href="${APP_PATH}/static/favicon.ico"/>
    <link rel="stylesheet" href="${APP_PATH}/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="${APP_PATH}/static/css/toastr.min.css">
    <link rel="stylesheet" href="${APP_PATH}/static/css/fontawesome.min.css">

    <link rel="stylesheet" href="${APP_PATH}/static/css/signin.css">

</head>
<body class="text-center">

<form class="form-signin">
    <img class="mb-4" src="${APP_PATH}/images/logo.jpg" alt="logo" width="72" height="72">
    <h1 class="h3 mb-3 font-weight-normal">后台管理系统&nbsp;&nbsp;<small>请登录</small></h1>
    <label for="username" class="sr-only">请输入用户名</label>
    <input type="text" id="username" class="form-control" placeholder="用户名" maxlength="16" name="username" required autofocus>

    <label for="password" class="sr-only">请输入密码</label>
    <input type="password" id="password" class="form-control" placeholder="密码" maxlength="16" name="password" required>
    <br/>

    <button id="login-btn" class="btn btn-lg btn-primary btn-block" type="button">登录</button>
</form>
</body>

<script src="${APP_PATH}/static/js/jquery-3.3.1.min.js"></script>
<script src="${APP_PATH}/static/js/bootstrap.min.js"></script>
<script src="${APP_PATH}/static/js/toastr.min.js"></script>
<script src="${APP_PATH}/static/js/jquery.cookie.js"></script>

<script>
    //正则验证字段
    function validate() {
        var regex_username = /^[a-zA-Z0-9]{4,16}$/;
        var regex_password = /^[a-zA-Z0-9]{8,16}$/;
        if (!regex_username.test($("input[name='username']").val())) {
            toastr.error("用户名格式不正确，由4-16位字母或数字组成！");
            return false;
        }

        if (!regex_password.test($("input[name='password']").val())) {
            toastr.error("密码格式不正确，由8-16位字母或数字组成！");
            return false;
        }
        return true;
    }



    //点击登录
    $("#login-btn").click(function () {
        if (validate()) {
            $.ajax({
                url: "/adminLogin",
                type: "POST",
                data: $("form").serialize(),
                success: function (result) {
                    if (result.code === 100) {
                        //登录成功，页面跳转
                        window.location.href="/adminNotification";
                    } else if (result.code ===200) {
                        //登录失败
                        toastr.error(result.extend.error);
                        return false;
                    }
                }
            });
        }
    });
</script>
</html>
